/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div class="item-title">
      <div class="subject">
        <h3>新闻分类</h3>
        <h5>网站新闻分类添加与管理</h5>
      </div>
    </div>
    <div
      id="explanation"
      class="explanation"
      style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;"
    >
      <div class="bckopa-tips">
        <div class="title">
          <img src="@/static/images/handd.png" alt />
          <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        </div>
        <ul>
          <li>新增新闻时，可选择新闻分类。新闻分类将在前台新闻列表页显示</li>
        </ul>
      </div>
      <span title="收起提示" id="explanationZoom" style="display: block;"></span>
    </div>
    <div class="mDiv">
      <div class="ftitle">
        <h3>分类列表</h3>
        <h5>(共 {{totalCount}} 条记录)</h5>
      </div>
      <div class="fbutton">
        <div class="add" @click="toAdd()">
          <span>
            <i class="el-icon-circle-plus" /> 新增分类
          </span>
        </div>
      </div>
      <a href @click.prevent="refresh()" class="refresh-date">
        <div title="刷新数据" class="pReload">
          <i class="fa fa-refresh"></i>
        </div>
      </a>
    </div>

    <div class="tablebox">
      <el-table :data="tableData" ref="multipleTable" style="width: 100%;">
        <el-table-column label="分类名称" prop="cat_name" width="200"></el-table-column>
        <el-table-column label="分类描述" prop="cat_desc" width="200">
          <template slot-scope="scope">{{scope.row.cat_desc || '--'}}</template>
        </el-table-column>
        <el-table-column label="排序" width="200">
          <template slot-scope="scope">
            <el-input
              class="sort-input"
              size="mini"
              v-model="scope.row.sort_order"
              @change="updateSort(scope.row)"
              onkeyup="this.value=this.value.replace(/[^\d.]/g,'');"
              maxlength="3"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button-group>
              <el-button @click="remove(scope.row.cat_id)" icon="el-icon-delete" size="small">删除</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      title="新增新闻分类"
      :visible.sync="dialogFormVisible"
      width="20%"
      center
      :modal-append-to-body="false"
    >
      <el-form ref="newsCat" :model="newsCat" label-width="100px">
        <el-form-item label="分类名称" required>
          <el-input v-model="newsCat.cat_name" size="mini"></el-input>
          <p class="err-msg" v-if="errResult.cat_name && !newsCat.cat_name">{{errResult.cat_name}}</p>
        </el-form-item>
        <el-form-item label="导航显示">
          <el-switch v-model="newsCat.show_in_nav" active-value="1" inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="newsCat.sort_order" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="搜索关键字">
          <el-input v-model="newsCat.keywords" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="搜索描述">
          <el-input v-model="newsCat.cat_desc" size="mini"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit()" :loading="isLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getNewCatList, updateNewsCat, removeNewsCat } from "@/utils/api";
export default {
  data() {
    return {
      totalCount: 0,
      tableData: [],
      newsCat: {},
      errResult: {},
      dialogFormVisible: false,
      isLoading: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      var that = this;
      getNewCatList().then(function(res) {
        that.tableData = res;
        that.totalCount = res.length;
      });
    },
    refresh() {
      this.getList();
      this.$message.success("刷新成功！");
    },
    updateSort(row) {
      var that = this;
      updateNewsCat({
        cat_id: row.cat_id,
        sort_order: row.sort_order
      }).then(function(res) {
        if (res.status == 1) {
          that.$message.success("更新成功");
          that.getList();
        }
      });
    },
    remove(catId) {
      var that = this;
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          removeNewsCat(catId).then(function(res) {
            if (res.status == 1) {
              that.$message.success("删除成功");
              that.getList();
            }
          });
        })
        .catch(() => {
          that.$message.info("已取消删除");
        });
    },
    toAdd() {
      this.newsCat = {};
      this.dialogFormVisible = true;
    },
    submit() {
      this.isLoading = true;
      var that = this;
      if (this.newsCat.cat_id) {
        //编辑
        this.request.put("/mall/news_cat", this.newsCat).then(function(res) {
          if (res.status == 1) {
            that.$message.success("保存成功");
            that.getList();
            that.dialogFormVisible = false;
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      } else {
        //添加
        this.request.post("/mall/news_cat", that.newsCat).then(function(res) {
          if (res.status == 1) {
            that.$message.success("添加成功");
            that.getList();
            that.dialogFormVisible = false;
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      }
    }
  }
};
</script>

<style scoped>
>>> .el-input .el-input__inner {
  width: 200px;
}
>>> .sort-input .el-input__inner {
  width: 60px;
}
</style>
